package com.xf.auth.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xf.common.core.entity.FebsResponse;
import com.xf.common.core.utils.FebsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xufeng
 * 登录成功处理
 */
@Slf4j
@Component
public class FebsWebLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    private final RequestCache requestCache = new HttpSessionRequestCache();
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        int status = HttpServletResponse.SC_OK;
        FebsResponse data = new FebsResponse();
        //状态=0  前端收到登录成功的状态码 再调整授权码地址 获取授权码
        data.code("0");
        FebsUtil.makeJsonResponse(response,status, data.message("登录成功"));
    }
}
